package asteroids.model;

import java.util.Random;

import be.kuleuven.cs.som.annotate.Basic;
import exception.IllegalRadiusException;

public class Asteroid extends SpaceObject {
	

	
    /**
     * Constructor for a new Asteroid. Initialize the constructor with ...
     * @param x
     * @param y
     * @param xVelocity
     * @param yVelocity
     * @param radius
     * @throws IllegalRadiusException
     * @throws IllegalArgumentException
     */
	public Asteroid(double x, double y, double xVelocity, double yVelocity, double radius) throws IllegalRadiusException,IllegalArgumentException{
		super(x,y,xVelocity,yVelocity,radius,SpaceObject.calculateMass(radius,Asteroid.getMassDensity()));
		random = null;
	}
	
	/**
	 * A constructor for a new Asteroid.
	 * @param x
	 * @param y
	 * @param xVelocity
	 * @param yVelocity
	 * @param radius
	 * @param random
	 * @throws IllegalRadiusException
	 * @throws IllegalArgumentException
	 */
	public Asteroid(double x, double y, double xVelocity, double yVelocity, double radius, Random random) 
			throws IllegalRadiusException,IllegalArgumentException{
		super(x,y,xVelocity,yVelocity,radius,SpaceObject.calculateMass(radius,Asteroid.getMassDensity()));
        this.random = random;

	}
	
	@Basic
	public Random getRandom(){
		return this.random;
	}
	
	private final Random random;

	@Basic
	public static double getMassDensity(){
		return massDensity;
	}
	
	private static final double massDensity = 2.65*Math.pow(10,12);
	
	public void terminate(){
		World oldWorld = this.getWorld();
		if(!isTerminated()){
			Random random = null;
			if(getRandom() == null)
				random = new Random();
			else random = getRandom();
		    if(this.getRadius() > 30){
			    double newRadius = this.getRadius()/2;
			    Vector motherPosition = this.getPosition();
			    Vector motherVelocity = this.getVelocity();
			    double direction1 = random.nextDouble()*Math.PI;
			    Vector child1Position = Vector.add(motherPosition, getRadius(), 0);
			    Vector child2Position = Vector.add(motherPosition, -getRadius(), 0); 
			    double speed = 1.5*Vector.scalarVector(motherVelocity.getX(), motherVelocity.getY());
			    Vector childV1 = Vector.thrust(new Vector(0,0), direction1, speed);
			    Vector childV2 = Vector.thrust(new Vector(0,0), direction1 + Math.PI, speed);
			    try{
				    if(this.hasWorld())
				    	unSetWorld();
				    this.isTerminated = true;
				    Asteroid asteroid1 = new Asteroid(child1Position.getX(), child1Position.getY(), childV1.getX(), childV1.getY(), newRadius,random);
				    Asteroid asteroid2 = new Asteroid(child2Position.getX(), child2Position.getY(), childV2.getX(), childV2.getY(), newRadius,random);	
				    oldWorld.addAsSpaceObject(asteroid1);
				    oldWorld.addAsSpaceObject(asteroid2);
			    }
			    catch(Exception exc){
			    }
		    }
		    if(this.hasWorld())
		    	unSetWorld();
		    this.isTerminated = true;
		}
	}

}